using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Manager : MonoBehaviour
{
    private int timer, spawnrate = 60;

    [SerializeField] private GameObject Tomato;

    private int Life = 5;

    private Text lifeText;
    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 60;
        lifeText = GameObject.Find("Life").GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        LifeManager();
    }

    private void FixedUpdate()
    {
        if (timer > spawnrate)
        {
            Instantiate(Tomato, this.transform.position, Quaternion.identity);
            timer = 0;
        }

        timer++;
    }

    void LifeManager()
    {
        //Game ends if life is down to zero
        if (Life <= 0)
        {
            SceneManager.LoadScene("SampleScene");
        }

        lifeText.text = "Life : " + Life.ToString();
    }


    public void LifeChange(int num)
    {
        Life += num;
    }
}
